function searchRange(nums, target) {
  const findFirst = (nums, target) => {
    let left = 0;
    let right = nums.length - 1;
    let result = -1;
    while (left <= right) {
      let mid = Math.floor((left + right) / 2);
      if (nums[mid] >= target) {
        right = mid - 1;
      } else if (nums[mid] < target) {
        left = mid + 1;
      }
      if (nums[mid] === target) {
        result = mid;
      }
    }
    return result;
  };

  const findLast = (nums, target) => {
    let left = 0;
    let right = nums.length - 1;
    let result = -1;

    while (left <= right) {
      let mid = Math.floor((left + right) / 2);
      if (nums[mid] <= target) {
        left = mid + 1;
      } else if (nums[mid] > target) {
        right = mid - 1
      }
      if (nums[mid] === target) {
        result = mid;
      }
    }
    return result;
  };

  const first = findFirst(nums, target);
  const last = findLast(nums, target);

  return [first, last]
}
const nums = [5, 7, 7, 8, 8, 10];
const target = 8;
console.log('结果:', searchRange(nums, target))
